<template>
  <div>
    <h2>用户添加</h2>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="名称" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model.number="ruleForm.mobile"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="ruleForm.gender">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
          <el-radio label="保密"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" :plain="true">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: '',
        mobile: '',
        email: '',
        gender: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur' }
        ],
        mobile: [{ validator: this.validateMobile, required: true, trigger: 'blur' }],
        email: [{ required: true, type: 'email', message: '请输入正确的电子邮箱', trigger: 'blur' }],
        gender: [{ required: true, message: '请选择性别', trigger: 'change' }]
      }
    }
  },
  methods: {
    // 验证手机号
    validateMobile(rule, value, callback) {
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else {
        const reg = /^1[3-9]\d{9}$/
        if (!reg.test(value)) {
          callback(new Error('请输入正确的手机号格式'))
        }
        callback()
      }
    },
    // 表单信息(对象)转字符串
    toStr(obj) {
      const arr = []
      for (const attr in obj) {
        arr.push(attr + '=' + obj[attr])
      }
      return arr.join('&')
    },
    // 表单提交事件
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 将表单中的数据深克隆
          const obj = JSON.parse(JSON.stringify(this.ruleForm))
          // 将性别替换成数字
          obj.gender = obj.gender === '男' ? 1 : obj.gender === '女' ? 2 : 3
          // console.log(obj)
          // 发送添加用户请求
          this.$http.post('/api/adduser', this.toStr(obj)).then((res) => {
            // console.log(res)
            if (res.errNo === 0 && res.message === 'success') {
              // 创建成功 - 重置表单
              this.resetForm(formName)
              // 成功的提示消息
              this.$message({
                message: '恭喜你，用户创建成功',
                type: 'success'
              })
            } else {
              this.$message.error('创建的挺好，下次别创建了')
              return false
            }
          })
        } else {
          this.$message.error('创建的挺好，下次别创建了哈哈哈')
          return false
        }
      })
    },
    // 表单重置事件
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style>
h2 {
  margin: 10px;
}
.el-form {
  width: 500px;
}
</style>
